import com.sun.javafx.scene.traversal.Algorithm;

import java.util.Arrays;
//有效三角形的个数
//测试链接  https://leetcode.cn/problems/valid-triangle-number/description/
public class TriangleNumber {
    //检查三个数是否能构成三角形
    public boolean check(int[] nums, int i, int j, int k){
        return   (nums[i] + nums[j]) > nums[k];
    }

    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);  //先对数值从大到小排序；
        int ans = 0;
        for(int max = nums.length-1; max > 1; max--){
            int right = max - 1;
            int left = 0;
            while(left < right){
                if(check(nums,left,right,max)){ //条件成立，right--
                    ans += (right-left);
                    right--;
                }else{
                    left++;
                }
            }
        }
        return ans;
    }

}
